package com.boc.ewr.business.service.impl;


import com.boc.ewr.bcimp.service.ICustomDepartService;
import com.boc.ewr.business.entity.EwrReceiptRatio;
import com.boc.ewr.business.entity.LineAccessList;
import com.boc.ewr.business.entity.bo.EwrReceiptRatioBO;
import com.boc.ewr.business.mapper.EwrReceiptRatioMapper;
import com.boc.ewr.business.service.IEwrReceiptRatioService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 智慧e对-对账指标考核
 * @Author: jeecg-boot
 * @Date: 2023-06-08
 * @Version: V1.0
 */
@Service
@Slf4j
public class EwrReceiptRatioServiceImpl extends ServiceImpl<EwrReceiptRatioMapper, EwrReceiptRatio> implements IEwrReceiptRatioService {

    @Autowired
    ICustomDepartService customDepartService;

    @Autowired
    EwrReceiptRatioMapper ewrReceiptRatioMapper;

    //批量导入自动生成数据
    @Override
    @Transactional
    public boolean createInfo() {
        //重点账单回执数
        List<EwrReceiptRatioBO> keyInfo = ewrReceiptRatioMapper.creatKeyInfo();
        //重点账单发送数
        List<EwrReceiptRatioBO> keyTotal = ewrReceiptRatioMapper.createKeyTotalInfo();
        //获取非重点账单回执数
        List<EwrReceiptRatioBO> nokeyInfo = ewrReceiptRatioMapper.createNoKeyInfo();
        //获取非重点账单发送数
        List<EwrReceiptRatioBO> nokeyTotal = ewrReceiptRatioMapper.createNoKeyTotalInfo();
        //网银回执总数（重点+非重点）
        List<EwrReceiptRatioBO> eBankInfo = ewrReceiptRatioMapper.createEbankInfo();
        //网银账单发送数（重点+非重点）
        List<EwrReceiptRatioBO> eBankTotal = ewrReceiptRatioMapper.createEbankTotalInfo();
        //转成Map键值对，键值为机构号
        //数据汇总
        Map<String, EwrReceiptRatio> maps = new HashMap<>();
        handleList(keyInfo, maps);
        handleList(keyTotal, maps);
        handleList(nokeyInfo, maps);
        handleList(nokeyTotal, maps);
        handleList(eBankInfo, maps);
        handleList(eBankTotal, maps);
        List<EwrReceiptRatio> resultList = new ArrayList<>(maps.values());
        //生成对应总数，比率，以及二级行名称
        assignmentInfo(resultList);
        saveBatch(resultList);
        //汇总每个分行的情况
        List<EwrReceiptRatioBO> branchInfo = ewrReceiptRatioMapper.createSumInfo();
        Map<String, EwrReceiptRatio> branchMap = new HashMap<>();
        handleList(branchInfo, branchMap);
        List<EwrReceiptRatio> branchList = new ArrayList<>(branchMap.values());
        assignmentInfo(branchList);
        log.info("功能：对账指标考核，插入数据量为{}！", resultList.size() + branchList.size());
        return saveBatch(branchList);
    }


    /**
     * 对数据进行 总数，比率，以及二级行名称
     *
     * @param resultList
     */
    private void assignmentInfo(List<EwrReceiptRatio> resultList) {
        for (EwrReceiptRatio ratio : resultList) {
            String keyBill = StringUtils.isEmpty(ratio.getKeyBillSend()) ? "0" : ratio.getKeyBillSend();
            BigDecimal keyBillSend = new BigDecimal(keyBill);
            String noKeyBill = StringUtils.isEmpty(ratio.getNokeyBillSend()) ? "0" : ratio.getNokeyBillSend();
            BigDecimal nokeyBillSend = new BigDecimal(noKeyBill);
            String ebankBill = StringUtils.isEmpty(ratio.getEbankBillSend()) ? "0" : ratio.getEbankBillSend();
            BigDecimal ebankBillSend = new BigDecimal(ebankBill);
            String keyBillRe = StringUtils.isEmpty(ratio.getKeyBillReceipt()) ? "0" : ratio.getKeyBillReceipt();
            BigDecimal keyBillReceipt = new BigDecimal(keyBillRe);
            String noKeyBillRe = StringUtils.isEmpty(ratio.getNokeyBillReceipt()) ? "0" : ratio.getNokeyBillReceipt();
            BigDecimal nokeyBillReceipt = new BigDecimal(noKeyBillRe);
            String ebankBillRe = StringUtils.isEmpty(ratio.getEbankBillReceipt()) ? "0" : ratio.getEbankBillReceipt();
            BigDecimal ebankBillReceipt = new BigDecimal(ebankBillRe);
            BigDecimal totalBillSend = keyBillSend.add(nokeyBillSend);
            DecimalFormat df = new DecimalFormat("0.00%");
            ratio.setTotalBillSend(totalBillSend.toString());
            if (keyBillSend.compareTo(BigDecimal.ZERO) == 0) {
                ratio.setKeyBillRatio("0.00%");
            } else {
                ratio.setKeyBillRatio(df.format(keyBillReceipt.divide(keyBillSend, 4, BigDecimal.ROUND_HALF_UP)));
            }
            if (nokeyBillSend.compareTo(BigDecimal.ZERO) == 0) {
                ratio.setNokeyBillRatio("0.00%");
            } else {
                ratio.setNokeyBillRatio(df.format(nokeyBillReceipt.divide(nokeyBillSend, 4, BigDecimal.ROUND_HALF_UP)));
            }
            if (ebankBillSend.compareTo(BigDecimal.ZERO) == 0) {
                ratio.setEbankUseRatio("0.00%");
            } else {
                ratio.setEbankUseRatio(df.format(ebankBillReceipt.divide(ebankBillSend, 4, BigDecimal.ROUND_HALF_UP)));
            }
            if (totalBillSend.compareTo(BigDecimal.ZERO) == 0) {
                ratio.setEbankReceiptRatio("0.00%");
            } else {
                ratio.setEbankReceiptRatio(df.format(ebankBillReceipt.divide(totalBillSend, 4, BigDecimal.ROUND_HALF_UP)));
            }
            if (StringUtils.isEmpty(ratio.getParentBranchNm())) {
                String parentBranchNm = customDepartService.getById(ratio.getBranchNo()).getDepartLeader();
                ratio.setParentBranchNm(parentBranchNm);
            }
        }

    }


    /**
     * 给数据汇总合并
     *
     * @param Info
     * @param map
     */
    private void handleList(List<EwrReceiptRatioBO> Info, Map<String, EwrReceiptRatio> map) {
        for (EwrReceiptRatioBO inf : Info) {
            if (map.get(inf.getBranchNo()) != null) {
                EwrReceiptRatio ratio = map.get(inf.getBranchNo());
                //发送数
                if (StringUtils.isNotEmpty(inf.getKeyBillSend())) ratio.setKeyBillSend(inf.getKeyBillSend());
                if (StringUtils.isNotEmpty(inf.getNokeyBillSend())) ratio.setNokeyBillSend(inf.getNokeyBillSend());
                if (StringUtils.isNotEmpty(inf.getEbankBillSend())) ratio.setEbankBillSend(inf.getEbankBillSend());
                //回执数
                if (StringUtils.isNotEmpty(inf.getKeyBillReceipt())) ratio.setKeyBillReceipt(inf.getKeyBillReceipt());
                if (StringUtils.isNotEmpty(inf.getNokeyBillReceipt()))
                    ratio.setNokeyBillReceipt(inf.getNokeyBillReceipt());
                if (StringUtils.isNotEmpty(inf.getEbankBillReceipt()))
                    ratio.setEbankBillReceipt(inf.getEbankBillReceipt());
            } else {
                //新增
                EwrReceiptRatio ratio = new EwrReceiptRatio();
                BeanUtils.copyProperties(inf, ratio);
                map.put(inf.getBranchNo(), ratio);
            }
        }
    }
}
